/*************************************************************************
	> File Name: week16-2.msg2.c
	> Author: Potter
	> Mail: tobewhatwewant@gmail.com 
	> Created Time: 2014年06月18日 星期三 15时06分31秒
 ************************************************************************/

#include<stdio.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/msg.h>
#include <sys/ipc.h>
#include <string.h>
#include <stdlib.h>

struct msgbuf {
    long mtype;
    char mtext[1024];
};

int main(int argc, char * argv[]) {
	int msqid;
    key_t key = 8;
    struct msgbuf out;

    while((msqid = msgget(key, IPC_CREAT | 0600)) == -1) {
        perror("Msgget");
        exit(-1);
    }
    fprintf(stdout, "Msqid = %d\n", msqid);
    
    out.mtype = 8;
    strcpy(out.mtext, "Hi, message1 is sending!");
    fprintf(stdout, "Hi, message1 is sending!\n");
    msgsnd(msqid, &out, sizeof(out.mtext), 0);

    strcpy(out.mtext, "Hi, message2 is sending!");
    fprintf(stdout, "Hi, message2 is sending!\n");
    // printf("Length: %d\n", sizeof(out.mtext));
    msgsnd(msqid, &out, sizeof(out.mtext), 0);

	return 0;
}
